clear
set more off
set type double
graph drop _all
program drop _all
set scheme s2mono
//log using "\\C:\Users\XUE XINDONG\Desktop\part3 educ_health_20191007", replace
use "C:\Users\XUE XINDONG\Desktop\educ_health\meta_educ_health(20190928).dta",

****************************************************************************
**************Robustness Checks: Z transformation  TABLE 10***************** 
****************************************************************************

******************************************************************************
*DEFINITION OF BASIC VARIABLES
******************************************************************************
gen tstat=tstatistics
gen negative = 1
replace negative = -1 if BadHealth == 1 & BadEduc == 0
replace negative = -1 if BadHealth == 0 & BadEduc == 1
replace tstat = tstat*negative
gen df = n-k1
gen r = tstat/sqrt(tstat^2+df)
gen varR = (1-r^2)/df
gen seR = sqrt(varR)
gen pcc = r
gen abststat = abs(tstat)
gen abspcc = abs(pcc)

******************************************************************************
* Distribution of t-stats and PCCs before kicking out outliers
******************************************************************************
summ tstat, detail
summ pcc, detail
summ df, detail 


******************************************************************************
*Kicking out outliers
******************************************************************************
reg pcc seR
predict resid, residual
egen stdr=std(resid)
gen absr=abs(stdr)
drop if absr>3.5
drop resid stdr absr

******************************************************************************
****Z Transformation of PCC and its Standard Errors**************************
*****************************************************************************
ge zpcc=0.5*ln((1+pcc)/(1-pcc))
gen zzpcc=atanh(pcc)
gen zseR=atanh(seR)
replace r=zzpcc
replace seR=zseR


******************************************************************************
* FAT/PET
******************************************************************************
******************************************************************************
* Calculating study weights based on number of estimates per study
******************************************************************************
bysort ID: egen numberests = count(ID)
gen weight = 1/numberests


******************************************************************************
* FIXED EFFECTS
******************************************************************************

gen feprecisionR = 1/seR
gen fetstatR = r/seR


******************************************************************************
* Equal weight to each estimate (Column 1)
******************************************************************************
regress fetstatR feprecisionR,  vce(cluster ID)


******************************************************************************
* Equal weight to each study (Column (2)
******************************************************************************
regress fetstatR feprecisionR  [pweight = weight],  vce(cluster ID)


******************************************************************************
* RANDOM EFFECTS 
******************************************************************************
//metan r seR, random
metareg r seR , wsse(seR)
scalar tau2 =  e(tau2)
gen revarR = varR + tau2
gen reseR = sqrt(revarR)


******************************************************************************
* Correcting for heteroskedasticity
******************************************************************************
gen reprecisionR = 1/reseR
gen retstatR = r/reseR
gen repubbiasR = seR/reseR


******************************************************************************
* Equal weight to each estimate (Column 3)
******************************************************************************
regress retstatR  reprecisionR repubbiasR,  noc vce(cluster ID)


******************************************************************************
* Equal weight to each study (Column 4)
******************************************************************************
regress retstatR  reprecisionR   repubbiasR [pweight = weight] , noc vce(cluster ID)



******************************************************************************
* *PEESE
******************************************************************************

******************************************************************************
* FIXED EFFECTS
******************************************************************************

******************************************************************************
* Equal weight to each estimate (Column 1)
******************************************************************************
regress fetstatR feprecisionR seR,  noc vce(cluster ID)


******************************************************************************
* Equal weight to each study (Column (2)
******************************************************************************
regress fetstatR feprecisionR seR [pweight = weight], noc vce(cluster ID)


******************************************************************************
* RANDOM EFFECTS 
******************************************************************************
ge  repubbiasRR = varR/reseR

******************************************************************************
* Equal weight to each estimate (Column 3)
******************************************************************************
regress retstatR  reprecisionR   repubbiasRR,  noc vce(cluster ID)


******************************************************************************
* Equal weight to each study (Column 4)
******************************************************************************
regress retstatR  reprecisionR repubbiasRR  [pweight = weight] , noc vce(cluster ID)


//log close
 